package com.usian.behavior.service.impl;

import com.usian.behavior.mapper.ApBehaviorEntryMapper;
import com.usian.behavior.mapper.ApLikeBehaviorMapper;
import com.usian.behavior.service.ApLikeBeaviorService;
import com.usian.model.behavior.dtos.LikesBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApLikeBeaviorServiceImpl implements ApLikeBeaviorService {

    @Autowired
    private ApLikeBehaviorMapper apLikeBehaviorMapper;
    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;

    @Override
    public ResponseResult saveLikeBeavior(LikesBehaviorDto dto) {

        if(dto==null){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //查询用户行为实体
        ApUser apUser = AppThreadLocalUtils.getUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.findByUserIdOrEquipmentId(apUser.getId(), null);
        if(apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.ENTRY_PARAM_INVALID);
        }

        //保存like。。。
        ApLikesBehavior apLikesBehavior =new ApLikesBehavior();
        apLikesBehavior.setEntryId(apBehaviorEntry.getId());
        apLikesBehavior.setType(dto.getType());
        apLikesBehavior.setArticleId(dto.getArticleId());
        apLikesBehavior.setOperation(dto.getOperation());
        apLikesBehavior.setCreatedTime(new Date());
        apLikeBehaviorMapper.saveLikeBehavior(apLikesBehavior);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
